package main

import (
	"encoding/json"
	"log"
	"panda-client/util"

	"github.com/go-resty/resty/v2"
)

//@author: luodesheng 308189510@qq.com
//@create: 2024-09-03 by vscode

type TerminalUbindResponse struct {
	Code   string `json:"code"`
	Msg    string `json:"msg,omitempty"`
	No     string `json:"no"`
	Passwd string `json:"passwd"`
	Mno    string `json:"mno"`
}

// 统一响应结构
type PandaApiResponse struct {
	Succcess bool                  `json:"success"`           // True请求成功， False请求失败
	ErrorMsg string                `json:"error"`             // 请求失败时，错误描述
	Payload  TerminalUbindResponse `json:"payload,omitempty"` // 业务参数json
}

var (
	// 接入方 2048bit rsa私钥
	privateKey = util.GetPrivateKey(util.Base64Decode("MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDd6TrkTjdhWmoBd8405mjWE37lgU8B0ro0oVIEJBDWEwbLLHQoUPztsV++IkLz4tApdOMC19evSZIuuigNLyogAmldN2ZwW7mfsqss5ftSnq6OI8Y6TcoYyURJvpTao8k6tbbiRdUKDWccX1acLZx7oaMDqmlQgz/K/26xQgarlu2eaArA8yZGSx6SUAwTZaMJeqaTtWxM85VfoVvOau17UWv1vULFLBnqS+pu3G3gtgHLSVYJfZHIPfk/EG/JDamjcQar42gCr83ITHzRuiQ3WIO+qpbliqjEy2RtMScvf+CqlQcJGr0/voTdsgs13OCpBmpc43fntQ0oUH9OQDPbAgMBAAECggEAFzfU4phQcQ4tszOezqgg5e0qRMcPxigPNXx/gVFotY1OE6n2CVXX8OKzGsio6q2bjArfyadduMhg1CTEFgUOSkUWUxt9y1m9hTkDcfIqJo3Vw6d4kRccgxVj4LdhCbNDVbI3jVszHBck57IN9M9M5R6XWMXWuQbSgOHS6hb3bbJt/fUa3rRDeQTZksbAGMHgeexYEAH3Az2vJ5FnlOaD3HAoxkRi9GBPULP3INGB88hqMH8OU0Jdqk8lE68fWqKnde+PwaawIktaLumQPDJBrkpab9NhypVnxxFF2tuW24Zq0mFZU/4PENT2iP3tHNozHH59lMqhw5iZ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rLOX7Up6ujiPGOk3KGMlESb6U2qPJOrW24kXVCg1nHF9WnC2ce6GjA6ppUIM/yv9usUIGq5btnmgKwPMmRkseklAME2WjCXqmk7VsTPOVX6Fbzmrte1Fr9b1CxSwZ6kvqbtxt4LYBy0lWCX2RyD35PxBvyQ2po3EGq+NoAq/NyEx80bokN1iDvqqW5YqoxMtkbTEnL3/gqpUHCRq9P76E3bILNdzgqQZqXON357UNKFB/TkAz2wIDAQAB"))
	//appId
	apppId = "b9eb18ac38d38aa5102aac4a72af10c6"
	//支持RSA
	keyType = "RSA"
)

func main() {

	/*
		终端绑定接口测试
	*/
	apiAddr := "http://192.168.12.1:30629/api/terminal/bind"
	requestJson := `
	{
		"mno": "816100057220001",
		"sn": "12341234123",
		"mfrsId": "JIN_HONG_LIN",
		"model": "HM600-v7",
		"channelId": "LS",
		"channelTno": "0000022"
	}
	`
	//上送国联支付的请求json
	log.Printf("req json >>: %s", requestJson)
	//签名计算
	hmacBytes := util.SignWithSha256(privateKey, []byte(requestJson))
	hmac := util.BytesToHex(hmacBytes)
	restClient := resty.New()
	restResp, err := restClient.R().SetHeader("App-Id", apppId).SetHeader("Key-Type", keyType).SetHeader("Content-Type", "application/json").SetHeader("Hmac", hmac).SetBody(requestJson).Post(apiAddr)
	if err != nil {
		log.Panic(err)
	}

	log.Printf("Waiting to receive response...")

	log.Printf("status code <<: %d", restResp.StatusCode())
	log.Printf("resp   body <<: %s", restResp.Body())

	respHmac := restResp.Header().Get("Hmac") // 国联私钥对body数据的签名(rsa with sha256)
	log.Printf("resp   hmac <<: %s", respHmac)

	if respHmac != "" {
		//验签
		if !util.VerifyWithSha256(publicKey, restResp.Body(), util.HexToBytes(respHmac)) {
			log.Panic("hmac校验不符")
		} else {
			log.Printf("hmac校验通过")
		}
	}

	var resp PandaApiResponse
	err = json.Unmarshal(restResp.Body(), &resp)
	if err != nil {
		log.Panic(err)
	}
	log.Printf("request success?: %t", resp.Succcess)
	if resp.Succcess {
		log.Printf("业务状态返回数据payload <<: %s", resp.Payload)
	}
}
